package com.thx.tags;

import com.opensymphony.xwork2.util.ValueStack;
import com.thx.common.spring.BeanFactory;
import com.thx.common.util.AppContext;
import com.thx.dictionary.model.DictionaryValue;
import com.thx.dictionary.service.DictionaryValueManager;
import com.thx.org.model.Company;
import com.thx.org.model.Department;
import com.thx.org.service.CompanyManager;
import com.thx.org.service.DepartmentManager;
import com.thx.resource.model.Resource;
import com.thx.resource.service.ResourceManager;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.components.Component;

import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * select标签的类.
 *
 * @author shiyan
 */
public class Select extends Component {

  private static final String SELECT_FORMAT = "<select %s>%s</select>";

  private static final String OPTION_FORMAT = "<option value=\"%s\" %s>%s</option>";

  private static final String TYPE_DEP = "dep";

  private static final String TYPE_DIC = "dic";

  private static final String TYPE_RESOURCE = "resource";

  private String name;

  private String type;

  private String value;

  private String dicCode;

  private boolean noDefault;

  private String valField;

  public Select(ValueStack stack) {
    super(stack);
  }

  @Override
  public boolean end(Writer writer, String body) {
    boolean result = super.start(writer);
    try {
      String options = "<option value=\"\">请选择</option>";
      if (noDefault) {
        options = "";
      }
      if (TYPE_DIC.equals(type)) {
        options += getOptionByDic();
      } else if (TYPE_DEP.equals(type)) {
        options += getOptionByDep();
      } else if (TYPE_RESOURCE.equals(type)) {
        options += getOptionByRescource();
      }
      String selectName = "";
      if (StringUtils.isNotBlank(name)) {
        selectName = "name=\"" + name + "\"";
      }
      String selectHtml = String.format(SELECT_FORMAT, selectName, options);
      writer.write(selectHtml);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }

  private String getOptionByRescource() {
    String options = "";
    ResourceManager resourceManager = (ResourceManager) BeanFactory.getBean("resourceManager");
    List<Resource> list = resourceManager.getByParentId(null);
    for (Resource r : list) {
      r.setOrderSeq(0);
      options += getChildrenRes(r);
    }
    return options;
  }

  private String getChildrenRes(Resource res) {
    if ("1".equals(res.getMenuAble())) {
      String optionText = "";
      for (int i = 0; i < res.getOrderSeq(); i++) {
        optionText += "&nbsp;&nbsp;";
        if (i == 0 && res.getOrderSeq() > 1) {
          optionText += "|";
        }
        if (i == res.getOrderSeq() - 1) {
          optionText += "|--";
        }
      }
      optionText += res.getName();

      String optionValue = res.getResourceId();

      String optionState = "class=" + "\"" + res.getCode() + "\"";

      if (StringUtils.isNotBlank(value) && value.equals(res.getResourceId())) {
        optionState += "  selected=\"selected\"";
      }
      String options = String.format(OPTION_FORMAT, optionValue, optionState, optionText);
      ResourceManager resourceManager = (ResourceManager) BeanFactory.getBean("resourceManager");
      List<Resource> children = resourceManager.getByParentId(res.getResourceId());
      if (children != null && children.size() > 0) {
        for (Resource child : children) {
          child.setOrderSeq(res.getOrderSeq() + 1);
          options += getChildrenRes(child);
        }
      }
      return options;
    }
    return "";
  }

  private String getOptionByDic() {
    String options = "";
    if (StringUtils.isNotBlank(dicCode)) {
      DictionaryValueManager mgr =
          (DictionaryValueManager) BeanFactory.getBean("dictionaryValueManager");
      List<DictionaryValue> dvs = mgr.findByDicnCode(dicCode);
      for (DictionaryValue dv : dvs) {
        String optionText = dv.getDicvText();
        String optionValue = dv.getDicValue();
        String optionState = "";
        if (StringUtils.isNotBlank(value) && value.equals(dv.getDicValue())) {
          optionState = "selected=\"selected\"";
        }
        options += String.format(OPTION_FORMAT, optionValue, optionState, optionText);
      }
    }
    return options;
  }

  private String getOptionByDep() {
    String comId = AppContext.getCurrentComId();
    CompanyManager companyMgr = (CompanyManager) BeanFactory.getBean("companyManager");
    Company company = companyMgr.get(comId);
    Department root = new Department();
    root.setDepId(null);
    root.setDepName(company.getComName());
    root.setFullName(company.getComName());
    root.setOrderSeq(0);

    return getChildrenDep(root);
  }

  private String getChildrenDep(Department dep) {
    String optionText = "";
    for (int i = 0; i < dep.getOrderSeq(); i++) {
      optionText += "&nbsp;&nbsp;";
      if (i == 0 && dep.getOrderSeq() > 1) {
        optionText += "|";
      }
      if (i == dep.getOrderSeq() - 1) {
        optionText += "|--";
      }
    }
    optionText += dep.getDepName();
    String optionValue = dep.getDepId();
    if (StringUtils.isNotBlank(valField)) {
      try {
        optionValue = BeanUtils.getProperty(dep, valField);
      } catch (IllegalAccessException e) {
      } catch (InvocationTargetException e) {
      } catch (NoSuchMethodException e) {
      }
    }
    optionValue = optionValue == null ? "" : optionValue;
    String optionState = "";
    if (StringUtils.isNotBlank(value) && value.equals(optionValue)) {
      optionState = "selected=\"selected\"";
    }
    if (StringUtils.isBlank(value) && dep.getDepId() == null) {
      optionState = "selected=\"selected\"";
    }
    String options = String.format(OPTION_FORMAT, optionValue, optionState, optionText);
    DepartmentManager mgr = (DepartmentManager) BeanFactory.getBean("departmentManager");
    List<Department> children = mgr.getByParentId(dep.getDepId());
    if (children != null && children.size() > 0) {
      for (Department child : children) {
        child.setOrderSeq(dep.getOrderSeq() + 1);
        options += getChildrenDep(child);
      }
    }
    return options;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public String getDicCode() {
    return dicCode;
  }

  public void setDicCode(String dicCode) {
    this.dicCode = dicCode;
  }

  public boolean getNoDefault() {
    return noDefault;
  }

  public void setNoDefault(boolean noDefault) {
    this.noDefault = noDefault;
  }

  public String getValField() {
    return valField;
  }

  public void setValField(String valField) {
    this.valField = valField;
  }

}
